﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//控制逻辑单边数据
public class Side : MonoBehaviour
{
    // Start is called before the first frame update
    //个人卡组
    public List<CardProps.ID> Deck = new List<CardProps.ID>();
    public List<Card> Hand = new List<Card>();
    public Card[] HiddenField;
    public Card[] ExposedField;
    public int HiddenValue = 0;
    public int ExposedValue = 0;
    public float Coin = 10;
    public void Init() {
        //Debug.Log("ExposedField0" + HiddenField[0]);
        //System.Enum.GetNames(CardProps.ID.GetType()).Length;
    }
    public void DrawCard() {
        if(Deck.Count >= 1) {
            CardProps.ID drawID = Deck[Deck.Count - 1];
            Deck.RemoveAt(Deck.Count - 1);
            Hand.Add(CardFactory.Instance.GetDataPrefab(drawID));
            VSUI.Instance.Draw(drawID, Hand.Count - 1);          
        } else {
            Debug.Log("No card!");
        }
    }
    public void RefreshCard() {
        VSUI.Instance.ClearHandCard();
        for(int i = 0; i < Hand.Count; ++i) {
            CardProps.ID drawID = Hand[i].ID;
            VSUI.Instance.Draw(drawID, i); 
        }
    }
    public Card SettleCard(int handIndex, Board.BoardType boardType, int boardIndex) {
        Debug.Log("handIndex" + handIndex);
        Debug.Log("boardIndex "+boardIndex);
        Card returnCard = null;
        //Debug.Log("ExposedField.Length "+ExposedField.Length);
        if(handIndex >= Hand.Count) {
            Debug.LogError("Hand Out of index!" + Hand.Count);
        }
        if(boardIndex >= 6) {
            Debug.LogError("Board Out of index!");
        }
        if(boardType == Board.BoardType.EXPOSED) {
            returnCard = ExposedField[boardIndex] = Hand[handIndex];
        } else if(boardType == Board.BoardType.HIDDEN) {
            returnCard = HiddenField[boardIndex] = Hand[handIndex];    
        }
        Hand.RemoveAt(handIndex);
        return returnCard;
    }
    void Update() {
        //string str = "";
        //foreach(Card card in Hand) {
        //    str += card.ID;
        //}
        //Debug.Log(str);
        CalExposedValue();
        CalHiddenValue();
        
    }
    void CalExposedValue() {
        ExposedValue = 0;
        foreach(Card card in ExposedField) {
            if(card != null) ExposedValue += card.CE;
        }
    }
    void CalHiddenValue() {
        HiddenValue = 0;
        foreach(Card card in HiddenField) {
            if(card != null) HiddenValue += card.CE;
        }
    }
    
}
